package edu.dillo.dillotude;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;

public class GridBlock extends Rectangle {
	
	
	/**
	 * Represents a block in the level that can be collided with. Doesn't have to be square, but probably should be.
	 */
	private static final long serialVersionUID = -5318820209829820890L;

	private boolean collidable;
	
	private Image currentImg;
	
	private SpriteSheet blockSprites;
	
	private float absoluteX, absoluteY, relativeX, relativeY;
	
	public enum BlockType{NONE,GRASS_NONE, GRASS_TOP, GRASS_SIDE, GRASS_TOP_SIDE, GRASS_CORNER_PAD, GRASS_TAPERED, GRASS_SIDE_F, GRASS_TOP_SIDE_F,GRASS_CORNER_PAD_F,GRASS_TAPERED_F};
	
	/**
	 * Define a block with custom coordinates and size
	 * @param x The x position of the block
	 * @param y The y position of the block
	 * @param width The width of the block
	 * @param height The height of the block
	 * @param iscollidable Whether this block can be collided with
	 * @param ss The sprite sheet containing the possible sprites this block can display
	 * @throws SlickException
	 */
	public GridBlock(float x, float y, float width, float height, boolean iscollidable, SpriteSheet ss) throws SlickException {
		super(x, y, width, height);
		blockSprites = ss;
		absoluteX = x;
		absoluteY = y;
		relativeX= absoluteX / getWidth();
		relativeY = absoluteY/ getHeight();
		
		setBlockImage(BlockType.NONE);
		
		
	}
	/**
	 * Define a square block
	 * @param x The x position of the block
	 * @param y The y position of the block
	 * @param size The side length of the block
	 * @param iscollidable Whether this block can be collided with
	 * @param ss The sprite sheet containing the possible sprites this block can display
	 * @throws SlickException
	 */
	public GridBlock(float x, float y, float size, boolean iscollidable, SpriteSheet ss) throws SlickException
	{
		super(x, y, size, size);
		blockSprites = ss;
		absoluteX = x;
		absoluteY = y;
		relativeX= absoluteX / getWidth();
		relativeY = absoluteY/ getHeight();
		setCollidable(iscollidable);
		setBlockImage(BlockType.NONE);
	}
	/**
	 * 
	 * @return The collidability of this block
	 */
	public boolean isCollidable() {
		return collidable;
	}
	/**
	 * 
	 * @param collidable The collidability of this block
	 */
	public void setCollidable(boolean collidable) {
		this.collidable = collidable;
	}
	
	public void init(GameContainer gc, StateBasedGame sbg)
			throws SlickException {
		blockSprites = new SpriteSheet(new Image("/media/player_sprites/dirt_blocks.png"), 100,100);
	}
	
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
			throws SlickException {
		if(currentImg != null)
			g.drawImage(currentImg, x, y);
	
	}
	/**
	 * Set the image that this block displays
	 * @param bt The type of block to be displayed
	 */
	void setBlockImage(BlockType bt)
	{
		switch(bt)
		{
		case GRASS_NONE:
			currentImg = blockSprites.getSprite(0, 0);
			break;
		case GRASS_TOP:
			currentImg = blockSprites.getSprite(1, 0);
			break;
		case GRASS_SIDE:
			currentImg = blockSprites.getSprite(2, 0);
			break;
		case GRASS_TOP_SIDE:
			currentImg = blockSprites.getSprite(3, 0);
			break;
		case GRASS_CORNER_PAD:
			currentImg = blockSprites.getSprite(4, 0);
			break;
		case GRASS_TAPERED:
			currentImg = blockSprites.getSprite(0, 1);
			break;
		case GRASS_SIDE_F:
			currentImg = blockSprites.getSprite(2, 0).getFlippedCopy(true, false);
			break;
		case GRASS_TOP_SIDE_F:
			currentImg = blockSprites.getSprite(3, 0).getFlippedCopy(true, false);
			break;
		case GRASS_CORNER_PAD_F:
			currentImg = blockSprites.getSprite(4, 0).getFlippedCopy(true, false);
			break;
		case GRASS_TAPERED_F:
			currentImg = blockSprites.getSprite(0, 1).getFlippedCopy(true, false);
			break;
		default:
			currentImg = null;
			break;
			
		}
	}
	/**
	 * 
	 * @return The y position of this block, in pixels, as it placed on the screen
	 */
	public float getAbsoluteY() {
		return absoluteY;
	}
	/**
	 * 
	 * @param absoluteY The y position of this block, in pixels, as it placed on the screen
	 */
	public void setAbsoluteY(int absoluteY) {
		this.absoluteY = absoluteY;
	}
	/**
	 * 
	 * @return The Y coordinate relative to the other blocks in the level.  In other words, the absolute Y divided by the height.
	 */
	public float getRelativeY() {
		return relativeY;
	}
	/**
	 * 
	 * @param relativeY The Y coordinate relative to the other blocks in the level.  In other words, the absolute Y divided by the height.
	 */
	public void setRelativeY(int relativeY) {
		this.relativeY = relativeY;
	}
	/**
	 * 
	 * @return The x position of this block, in pixels, as it placed on the screen
	 */
	public float getAbsoluteX() {
		return absoluteX;
	}
	/**
	 * 
	 * @param absoluteX The x position of this block, in pixels, as it placed on the screen
	 */
	public void setAbsoluteX(int absoluteX) {
		this.absoluteX = absoluteX;
	}
	/**
	 * 
	 * @return The X coordinate relative to the other blocks in the level.  In other words, the absolute Y divided by the height.
	 */
	public float getRelativeX() {
		return relativeX;
	}
	/**
	 * The X coordinate relative to the other blocks in the level.  In other words, the absolute Y divided by the height.
	 * @param relativeX
	 */
	public void setRelativeX(int relativeX) {
		this.relativeX = relativeX;
	}
}
